package com.neusoft.bizcore.activiti.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.activiti.converter.ProcessFormItemConverter;
import com.neusoft.bizcore.activiti.dto.ProcessFormItemDTO;
import com.neusoft.bizcore.activiti.model.ProcessFormItem;
import com.neusoft.bizcore.activiti.repository.ProcessFormItemRepository;
import com.neusoft.bizcore.activiti.service.ProcessFormItemService;
import com.neusoft.bizcore.web.support.Searchable;

@Service
public class ProcessFormItemServiceImpl implements ProcessFormItemService {

    @Autowired
    private ProcessFormItemRepository processFormItemRepository;
    @Autowired
    private ProcessFormItemConverter processFormItemConverter;

    @Override
    public List<ProcessFormItemDTO> getFormItemByFormCode(final String formCode) {
        final List<ProcessFormItem> models = this.processFormItemRepository.findByFormCode(formCode);
        return this.processFormItemConverter.toListDTO(models);
    }

    @Override
    public ProcessFormItemDTO create(final ProcessFormItemDTO dto) {
        final ProcessFormItem model = this.processFormItemConverter.toModel(dto);
        this.processFormItemRepository.save(model);
        return this.processFormItemConverter.toDTO(model);
    }

    @Override
    public void deleteByFormCode(final String code) {
        this.processFormItemRepository.deleteByFormCode(code);
    }

    @Override
    public List<ProcessFormItemDTO> index(final Searchable searchable) {
        final List<ProcessFormItem> models = this.processFormItemRepository.index(searchable);
        return this.processFormItemConverter.toListDTO(models);
    }

    @Override
    public Page<ProcessFormItemDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<ProcessFormItem> result = this.processFormItemRepository.search(pageable, searchable);
        return this.processFormItemConverter.toPageDTO(result);
    }

}
